home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / vol7n16.arc / SELFMOD.PAS < prev    next >
Pascal/Delphi Source File  |  1988-07-18  |  1KB  |  37 lines

  1. PROGRAM Self_Modify;          { Compile to a .COM file before execution }
  2.  
  3. CONST
  4.   StoredValue : STRING[80] = 'initial value of stored string';
  5.   ProgramName : STRING[64] = 'SELFMOD.COM';
  6.  
  7. VAR
  8.   F : FILE OF Byte;
  9.   I : Integer;
  10.  
  11. BEGIN
  12.   Assign(F, ProgramName);     { open .COM file }
  13.   {$I-} Reset(F); {$I+}
  14.   IF IOResult = 0 THEN        { check for i/o error }
  15.     BEGIN
  16.       {$I-} Seek(F, Ofs(StoredValue)-$100);
  17.       {$I+}                   { get location of StoredValue }
  18.       IF IOResult <> 0 THEN   { (a typed constant) in Cseg  }
  19.         BEGIN
  20.           WriteLn('Seek failed.');
  21.           Close(F);
  22.           Halt(1);            { abort with ErrorLevel=1 }
  23.         END;
  24.       WriteLn('Stored value was ->', StoredValue, '<-');
  25.       Write('Enter new value  ->'); Read(StoredValue); WriteLn('<-');
  26.       FOR I := Ofs(StoredValue) TO Ofs(ProgramName) DO
  27.                                { write new value of    }
  28.         Write(F, Mem[CSeg:I]); { StoredValue into file }
  29.       Close(F);
  30.     END
  31.   ELSE
  32.     BEGIN
  33.       WriteLn('Could not open file ',ProgramName);
  34.       Halt(2);                 { abort with ErrorLevel=2 }
  35.     END;
  36. END.
  37.